<template>
  <a-modal
    :title="title"
    :width="640"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :maskClosable="false"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          v-show="false"
          label="id"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-input
            :disabled="showable"
            v-decorator="['id']" />
        </a-form-item>
        <a-form-item
          label="费用类型编码"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-input
            :disabled="editable||showable"
            v-decorator="['feeTypeCode', {rules: [{required: true, message: '请输入费用类型编码'}]}]" />
        </a-form-item>
        <a-form-item
          label="费用类型名称"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-input
            :disabled="editable||showable"
            v-decorator="['feeTypeName', {rules: [{required: true, message: '请输入费用类型名称'}]}]" />
        </a-form-item>
        <a-form-item
          label="类型"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-select
            :disabled="showable"
            :options="feeType"
            v-decorator="['feeType', {rules: [{required: true, message: '请输入类型'}]}]">
          </a-select>
        </a-form-item>
        <a-form-item
          label="金额"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-input-number
            style="width: 100%"
            :min="0"
            :disabled="showable"
            v-decorator="['amount', {rules: [{required: true, message: '请输入金额'}]}]" />
        </a-form-item>
        <a-form-item
          label="备注 "
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-textarea
            :disabled="showable"
            v-decorator="['remarks', {rules: [{required: false, message: '请输入备注'}]}]" />
        </a-form-item>
      </a-form>
    </a-spin>
    <template slot="footer">
      <div v-show="editable || addable">
        <a-button key="cancel" @click="handleCancel">取消</a-button>
        <a-button key="forward" :loading="confirmLoading" type="primary" @click="handleOk">保存</a-button>
      </div>
    </template>
  </a-modal>
</template>

<script>
import { FormMixin } from '@/mixins/FormMixin'
import pick from 'lodash.pick'
import { addObj, getObj, putObj } from '@/api/base/baseFeeTypeBase'

export default {
  name: 'baseFeeTypeAddOrUpdate',
  mixins: [FormMixin],
  data() {
    return {
      feeType:[
        {label: "支出", value: "支出"},
        {label: "收入", value: "收入"}
      ],
      confirmLoading: false,
      warehouseTypes: [],
      form: this.$form.createForm(this)
    }
  },
  methods: {
    edit(id, type) {
      this.visible = true
      const { form: { setFieldsValue, resetFields } } = this
      if (['edit', 'show'].includes(type)) {
        this.confirmLoading = true
        getObj(id).then(res => {
          const record = res.data
          if (record.category === 'lineSite') {
            this.typeChange(record.category)
          }
          this.confirmLoading = false
          this.$nextTick(() => {
            setFieldsValue(pick(record, ['id',  'feeTypeCode','feeTypeName', 'feeType', 'amount', 'remarks']))
          })
        })
      } else {
        resetFields()
      }
    },
    // 过滤
    filterOption(input, option) {
      return (
        option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
      )
    },
    //点击保存，执行的方法
    handleOk() {
      this.form.validateFields(async (err, values) => {
        if (!err) {
          this.confirmLoading = true
          if (this.type === 'add') {
            addObj(values).then(res => {
              if (res.code === 500||res.code === 300) {
                this.confirmLoading = false
                this.$message.error(res.message)
              } else {
                this.confirmLoading = false
                this.$message.success('操作成功')
                this.$emit('ok')
                this.handleCancel()
              }
            })
          } else if (this.type === 'edit') {
            putObj(values).then(res => {
              if (res.code === 500||res.code === 300) {
                this.confirmLoading = false
                this.$message.error(res.message)
              } else {
                this.confirmLoading = false
                this.$message.success('操作成功')
                this.$emit('ok')
                this.handleCancel()
              }

            })
          }
        }
      })
    }
  }
}
</script>

<style scoped>

</style>